package com.neidetcher.tuml.model;

import java.util.List;

/**
 * order is important but it is assumed that messages are kept
 * in a container such that their order will be preserved.
 * 
 * TODO:
 * This should be a lot more rich than it is, it should carry
 * around references to messages this call makes and the messages
 * should carry around objects for the from and to.
 */
public class Message
{
   // manager.findCustomer()->dao.findCustomer(String customerName):Customer
   public Message(String rawMessage)
   {
      int arrowLoc = rawMessage.indexOf("->");
      
      String fromPart = rawMessage.substring(0, arrowLoc);
      String toPart = rawMessage.substring(arrowLoc + 2, rawMessage.length());
      
      System.out.println("from part: " + fromPart);
      System.out.println("to part: " + toPart);
      
      parseFromPart(fromPart);
      parseToPart(toPart);
   }
   
   // go thru the messages and figure out the lifespan of each message
   public static void assignLifespan(List<Message> messagesIn)
   {
//      for(int ii = 0; ii < messagesIn.size(); ii++)
//      {
//         String toObject = messagesIn.get(ii).getToObject();
//         String toMethod = messagesIn.get(ii).getToObject();
//         
//         
//      }
      
   }

   // manager.findCustomer()
   private void parseFromPart(String fromPart)
   {
      int dotLoc = fromPart.indexOf(".");
      int parenLoc = fromPart.indexOf("()");

      if (parenLoc == -1 || dotLoc == -1)
      {
         fromObject = "";
         fromMethod = "";
         return;
//         throw new IllegalArgumentException(
//               "from part of message didn't have a '()' or '.', must be of form <from object>.<from method>()");
      }
      
      fromObject = fromPart.substring(0, dotLoc);
      fromMethod = fromPart.substring(dotLoc + 1, parenLoc);
   }
   
   // dao.findCustomer(String customerName):Customer
   private void parseToPart(String toPart)
   {
      int dotLoc = toPart.indexOf(".");
      int openParenLoc = toPart.indexOf("(");
      int closeParenLoc = toPart.indexOf(")");
      int colonLoc = toPart.indexOf(":");
      
      if (dotLoc == -1 || openParenLoc == -1 || closeParenLoc == -1 || colonLoc == -1)
      {
         System.out.println("problem parsing to part");
         System.out.println("toPart: " + toPart);
         throw new IllegalArgumentException(
               "to part of message was missing one or more of [., (, ), :] should be of the form " + 
               "<to object>.<to method>(<arguments>):<return type>");
      }
      
      toObject   = toPart.substring(0, dotLoc);
      toMethod   = toPart.substring(dotLoc + 1, openParenLoc);
      arguments  = toPart.substring(openParenLoc + 1, closeParenLoc);
      returnType = toPart.substring(colonLoc + 1, toPart.length());
   }
   
   private String fromObject;
   private String fromMethod;
 
   private String toObject;
   private String toMethod;
   private String arguments;
   private String returnType;
   
   private int lifeSpan;
   
   public String getArguments()
   {
      return arguments;
   }
   public void setArguments(String arguments)
   {
      this.arguments = arguments;
   }
   public String getFromMethod()
   {
      return fromMethod;
   }
   public void setFromMethod(String fromMethod)
   {
      this.fromMethod = fromMethod;
   }
   public String getFromObject()
   {
      return fromObject;
   }
   public void setFromObject(String fromObject)
   {
      this.fromObject = fromObject;
   }
   public String getReturnType()
   {
      return returnType;
   }
   public void setReturnType(String returnType)
   {
      this.returnType = returnType;
   }
   public String getToMethod()
   {
      return toMethod;
   }
   public void setToMethod(String toMethod)
   {
      this.toMethod = toMethod;
   }
   public String getToObject()
   {
      return toObject;
   }
   public void setToObject(String toObject)
   {
      this.toObject = toObject;
   }
   
   public String toString()
   {
      String outStr = new String("Message");
      outStr += "\n\tfromObject :" + fromObject;
      outStr += "\n\tfromMethod :" + fromMethod;
      outStr += "\n\ttoObject   :" + toObject  ;
      outStr += "\n\ttoMethod   :" + toMethod  ;
      outStr += "\n\targuments  :" + arguments ;
      outStr += "\n\treturnType :" + returnType;
      outStr += "\n\tlifeSpan   :" + lifeSpan;
      return outStr;
   }

   public int getLifeSpan()
   {
      return lifeSpan;
   }

   public void setLifeSpan(int lifeSpan)
   {
      this.lifeSpan = lifeSpan;
   }
}
